package com.tuling.jucdemo.lock;
/**
 * CASLocl工具类
 */
import com.tuling.jucdemo.factory.UnsafeFactory;
import sun.misc.Unsafe;

public class CASLock {
    //加锁的标记
    private volatile int state;
    private static final Unsafe UNSAFE;
    private static final long OFFSET;

    static{
        try {
            UNSAFE = UnsafeFactory.getUnsafe();
            OFFSET = UnsafeFactory.getFieldOffset(UNSAFE,CASLock.class, "state");
        }catch (Exception e){
            throw new Error(e);
        }
    }

    public boolean cas(){
        return UNSAFE.compareAndSwapInt(this, OFFSET, 0, 1);
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}
